#include "config.h"
#include <QDebug>

Config::Config(QObject *parent):QObject(parent)
{
    init(CFG_FILENAME);
}

Config::Config(const QString& filename,QObject* parent)
    :QObject(parent)
{
    init(filename);
}

Config::~Config()
{
    delete file;
    delete textStream;
}

QString Config::getFilename()const
{
    return file->fileName();
}

void Config::setFilename(const QString &filename)
{
    file->setFileName(filename);
    file->open(QFile::ReadOnly|QFile::WriteOnly);      //open the file
}

bool Config::deleteFile()
{
    return file->remove();
}

void Config::init(const QString &filename)
{
    file=new QFile(filename);        //set filename and open file
    if(!file->exists())              //warning, file does not exist
        qWarning()<<"Config-Datei "+file->fileName()+" existiert nicht. Erstelle neue leere Config-Datei.";

    file->open(QFile::ReadOnly|QFile::WriteOnly);      //open the file
    textStream=new QTextStream(file);  //attach to text stream
    //qDebug()<<"File: "<<textStream->readAll();
}

void Config::addVar(const QString &key, const QString &var)
{
    QString line;
    int i=-1;

    qDebug()<<"Lese Zeilen aus...";

    /*read all lines until end*/
    textStream->seek(0);
    while(!(line=textStream->readLine()).isEmpty())
    {
        qDebug()<<"line: "<<line;
        if((i=line.indexOf("="))==-1)continue;           //error with line, not the right format, skip
        qDebug()<<"line.left("<<i<<"): "<<line.left(i);
        if(line.left(i)==key)                          //if key found
        {
            i=textStream->pos()-2;                  //save pos before \r\n
            qDebug()<<"POS: "<<textStream->pos();
            QString addStr=textStream->readAll();       //read all bytes from pos
            textStream->seek(i);                        //seek to start pos
            *textStream<<","<<var<<"\r\n";              //insert the var to the key
            qDebug()<<"POS: "<<textStream->pos();
            *textStream<<addStr;                        //add the bytes after the inserted var
            return;
        }
    }
    qDebug()<<"Erstelle neuen key";

    *textStream<<key<<"="<<var<<"\r\n";      //no key found, add key
    qDebug()<<"key erstellt";
}

void Config::addVar(const QString &key, const QStringList &vars)
{
    QString line;
    int i=-1;

    qDebug()<<"Lese Zeilen aus...";

    /*read all lines until end*/
    textStream->seek(0);
    while(!(line=textStream->readLine()).isEmpty())
    {
        qDebug()<<"line: "<<line;
        if((i=line.indexOf("="))==-1)continue;           //error with line, not the right format, skip
        qDebug()<<"line.left("<<i<<"): "<<line.left(i);
        if(line.left(i)==key)                          //if key found
        {
            i=textStream->pos()-2;                  //save pos before \r\n
            qDebug()<<"POS: "<<textStream->pos();
            QString addStr=textStream->readAll();       //read all bytes from pos
            textStream->seek(i);                        //seek to start pos
            *textStream<<","<<vars.join(",")<<"\r\n";              //insert the var to the key
            qDebug()<<"POS: "<<textStream->pos();
            *textStream<<addStr;                        //add the bytes after the inserted var
            return;
        }
    }
    qDebug()<<"Erstelle neuen key";

    *textStream<<key<<"="<<vars.join(",")<<"\r\n";      //no key found, add key
    qDebug()<<"key erstellt";
}

QStringList Config::getVar(const QString &key)
{
    QString line;
    int i=-1;

    /*read all lines until end*/
    textStream->seek(0);
    while(!(line=textStream->readLine()).isEmpty())
    {
        if((i=line.indexOf("="))==-1)continue;           //error with line, not the right format, skip

        if(line.left(i)==key)                          //if key found
        {
            qDebug()<<"Gefundene Zeile: "<<line;
            return line.mid(i+1).split(",");    //return the vars of the key in a QStringList
        }
    }
    return QStringList();
}
